package com.yuqing.sentimentapp.widget.xrecyclerview;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.widget.xrecyclerview.progressindicator.AVLoadingIndicatorView;


public class LoadingMoreFooter extends LinearLayout {

    private SimpleViewSwitcher progressCon;
    public final static int STATE_LOADING = 0;
    public final static int STATE_COMPLETE = 1;
    public final static int STATE_NOMORE = 2;

    private TextView mText;
    private String loadingHint;
    private String noMoreHint;
    private String loadingDoneHint;

    private AVLoadingIndicatorView progressView;

    public LoadingMoreFooter(Context context) {
        super(context);
        initView();
    }

    /**
     * @param context
     * @param attrs
     */
    public LoadingMoreFooter(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public void destroy() {
        progressCon = null;
        if (progressView != null) {
            progressView.destroy();
            progressView = null;
        }
    }

    public void setLoadingHint(String hint) {
        loadingHint = hint;
    }

    public void setNoMoreHint(String hint) {
        noMoreHint = hint;
    }

    public void setLoadingDoneHint(String hint) {
        loadingDoneHint = hint;
    }

    public void initView() {
        setGravity(Gravity.CENTER);
        setLayoutParams(new RecyclerView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        progressCon = new SimpleViewSwitcher(getContext());
        progressCon.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

        View view = ((LayoutInflater) (getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)))
                .inflate(R.layout.foot_mall_recycler, null);
        progressCon.setView(view);

        //        progressView = new  AVLoadingIndicatorView(this.getContext());
        //        progressView.setIndicatorColor(0xffB5B5B5);
        //        progressView.setIndicatorId(ProgressStyle.BallSpinFadeLoader);
        //        progressCon.setView(progressView);
        mText = (TextView) view.findViewById(R.id.tv_content);
        addView(progressCon);

        //        progressCon.setView(new ProgressBar(getContext(), null, android.R.attr.progressBarStyle));

        //        mText = new TextView(getContext());
        //        mText.setText("loading");

        if (loadingHint == null || loadingHint.equals("")) {
            loadingHint = "正在加载";
        }
        if (noMoreHint == null || noMoreHint.equals("")) {
            noMoreHint = "我是有底线的";
        }
        if (loadingDoneHint == null || loadingDoneHint.equals("")) {
            loadingDoneHint = "正在加载";
        }
    }

    //    public void setProgressStyle() {
    //        progressCon.setView(new ProgressBar(getContext(), null, android.R.attr.progressBarStyle));
    //    }

    public void setState(int state) {
        switch (state) {
            case STATE_LOADING:
                progressCon.setVisibility(View.VISIBLE);
                mText.setText(loadingHint);
                this.setVisibility(View.VISIBLE);
                break;
            case STATE_COMPLETE:
                mText.setText(loadingDoneHint);
                this.setVisibility(View.GONE);
                break;
            case STATE_NOMORE:
                mText.setText(noMoreHint);
//                progressCon.setVisibility(View.GONE);
                this.setVisibility(View.VISIBLE);
                break;
        }
    }
}
